﻿using BCCommon;
using BCCommon.Enums;
using BCDto.TMS.OrderSpecificServiceRecord;
using BCEntity.TMS.OrderSpecificServiceRecord;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCData.TMS.Order.OrderSpecificServiceRecord;

namespace BCService.TMS.OrderSpecificServiceRecord
{
	public class OrderSpecificServiceRecordService : IOrderSpecificServiceRecordService
	{
		private readonly IDatabaseContext databaseContext;
		private readonly IRedisService redisService;
		private readonly IOrderSpecificServiceRecordData orderSpecificServiceRecordData;
		private readonly string key = string.Format("{0}", EntityNameConst.OrderSpecificServiceRecordEntity);

		public OrderSpecificServiceRecordService(IDatabaseContext databaseContext,
			IRedisService redisService,
			IOrderSpecificServiceRecordData orderSpecificServiceRecordData)
		{
			this.databaseContext = databaseContext;
			this.redisService = redisService;
			this.orderSpecificServiceRecordData = orderSpecificServiceRecordData;
		}

		#region 检查订单特殊服务记录是否存在
		public bool Exists(long orderSpecificServiceRecordId)
		{
			return this.orderSpecificServiceRecordData.Exists(orderSpecificServiceRecordId).GetAwaiter().GetResult();
		}
		#endregion

		#region 添加订单特殊服务记录
		public OrderSpecificServiceRecordDto Add(OrderSpecificServiceRecordRequestDto requestDto)
		{
			var entity = requestDto.As<OrderSpecificServiceRecordEntity>();

			return this.orderSpecificServiceRecordData.Add(entity).GetAwaiter().GetResult().As<OrderSpecificServiceRecordDto>();
		}
		#endregion

		#region 修改订单特殊服务记录
		public OrderSpecificServiceRecordDto Update(OrderSpecificServiceRecordPutDto putDto)
		{
			if (!this.Exists(putDto.OrderSpecificServiceRecordId))
			{
				throw new ArgumentException("此记录不存在!");
			}
			var entity = putDto.As<OrderSpecificServiceRecordEntity>();
			var result = this.orderSpecificServiceRecordData.Update(entity).GetAwaiter().GetResult().As<OrderSpecificServiceRecordDto>();
			string cacheKey = this.redisService.GetKey(key, entity.OrderSpecificServiceRecordId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 删除订单特殊服务记录
		public bool Delete(long orderSpecificServiceRecordId)
		{
			if (!this.Exists(orderSpecificServiceRecordId))
			{
				throw new ArgumentException("此记录不存在!");
			}

			var result = this.orderSpecificServiceRecordData.Delete(orderSpecificServiceRecordId).GetAwaiter().GetResult();
			string cacheKey = this.redisService.GetKey(key, orderSpecificServiceRecordId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 获取订单特殊服务记录
		public OrderSpecificServiceRecordDto Get(long orderSpecificServiceRecordId)
		{
			string cacheKey = this.redisService.GetKey(key, orderSpecificServiceRecordId);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.orderSpecificServiceRecordData.GetEntity(orderSpecificServiceRecordId).GetAwaiter().GetResult();
				return data;
			}, TimeSpan.FromDays(1));
			return result.As<OrderSpecificServiceRecordDto>();
		}
		#endregion

		#region 获取订单特殊服务记录列表
		public IEnumerable<OrderSpecificServiceRecordDto> GetList(long orderId)
		{
			string cacheKey = this.redisService.GetKey(key, orderId);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				return this.orderSpecificServiceRecordData.GetList(orderId).GetAwaiter().GetResult();
			}, TimeSpan.FromDays(1));
			return result.As<IEnumerable<OrderSpecificServiceRecordDto>>();
		}
		public IEnumerable<OrderSpecificServiceRecordDto> GetList(string orderNo)
		{
			string cacheKey = this.redisService.GetKey(key, orderNo);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				return this.orderSpecificServiceRecordData.GetList(orderNo).GetAwaiter().GetResult();
			}, TimeSpan.FromDays(1));
			return result.As<IEnumerable<OrderSpecificServiceRecordDto>>();
		}
		#endregion

		#region 获取订单特殊服务记录分页
		public PageableList<OrderSpecificServiceRecordDto> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			string cacheKey = this.redisService.GetKey(key, start, end, sortName, ascending, pageIndex, pageSize);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.orderSpecificServiceRecordData.Query(start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
				return new PageableList<OrderSpecificServiceRecordDto>
				{
					Count = data.Item2,
					Items = data.Item1.As<IEnumerable<OrderSpecificServiceRecordDto>>(),
					PageIndex = pageIndex,
					PageSize = pageSize,
					ServerTime = DateTime.Now
				};
			}, TimeSpan.FromMinutes(30));

			return result;
		}
		#endregion
	}
}